package com.leo.springbootmultithreadbasic.java8;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;

/**
 * @author Leo
 * @version V1.0
 */
public class ExceptionHandleDemo {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandleDemo.class);


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test();
    }

    /**
     * 测试场景：出现异常，并进行异常处理
     */
    public static void test() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            // 模拟出现异常
            int a =  1 / 0;
        });

        future.handle((BiFunction<Void, Throwable, Void>) (aVoid, throwable) -> {

            if (null != throwable) {
                logger.error("异常：", throwable);
            }

            return null;
        });

        future.get();

    }
}
